﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private string connString = @"Server=.\SqlExpress; Integrated Security = true; Initial Catalog = DB";
        public SqlServerDbDataContext UserDataContext
        {
            get;
            set;
        }

        public Window1()
        {
            InitializeComponent();


            UserDataContext = new SqlServerDbDataContext(connString);
            InitData();
            //dgUsers.Columns[0].Visibility = Visibility.Collapsed;
        }

        public void InitData()
        {

            var users = from user in UserDataContext.Users
                        select user;

            dgUsers.ItemsSource = users;
            //dgUsers.Items.RemoveAt(dgUsers.Items.Count - 1);
            //int a = dgUsers.Items.Count;
            //dgUsers.
            //dgUsers.Columns[0].Width = dgUsers.Width * .3;
            //dgUsers.Columns[1].Width = dgUsers.Width * .7;
            //dgUsers.AutoGenerateColumns = true;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            //var u = (from user
            //           in dc.Users

            //         select user).SingleOrDefault();

            //if (dgUsers.Items.Count > 0)
            //{
            //    var u2 = dc.Users.Take(1).Select(p => p).SingleOrDefault();
            //    dc.Users.DeleteOnSubmit(u2);
            //    dc.SubmitChanges();

            //    Init();
            //}
            //ctrlAddUser.Visibility = Visibility.Visible;
            LayoutRoot.Children.Add(new UserControl1(this));
        }

        private void btnDelete_MouseEnter(object sender, MouseEventArgs e)
        {
            //Storyboard sb = new Storyboard();
            //DoubleAnimation dbResize = new DoubleAnimation(btnDelete.Width * 2, TimeSpan.FromSeconds(1));
            ////dbResize.SetValue(Storyboard.TargetNameProperty, btnDelete);
            //Storyboard.SetTarget(dbResize, btnDelete);
            //Storyboard.SetTargetProperty(dbResize, new PropertyPath(Canvas.WidthProperty));

            //sb.Children.Add(dbResize);


            //sb.Begin();
        }

        private void btnDelete_MouseLeave(object sender, MouseEventArgs e)
        {
            //Storyboard sb = new Storyboard();
            //DoubleAnimation dbResize = new DoubleAnimation(btnDelete.Width / 2, TimeSpan.FromSeconds(1));
            ////dbResize.SetValue(Storyboard.TargetNameProperty, btnDelete);
            //Storyboard.SetTarget(dbResize, btnDelete);
            //Storyboard.SetTargetProperty(dbResize, new PropertyPath(Canvas.WidthProperty));

            //sb.Children.Add(dbResize);


            //sb.Begin();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            User currentUser = dgUsers.SelectedItem as User;
            UserDataContext.Users.DeleteOnSubmit((User)currentUser);
            UserDataContext.SubmitChanges();
            InitData();
        }

        private void dgUsers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //e.
        }


    }
}
